﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class DeleteUser : System.Web.UI.Page
{
    string userName;
    Users currentUser = new Users();
    DBOperation db = new DBOperation();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] == null)
        {
            Response.Redirect(GetUrlPath.GetUrl + "/Login.aspx");
        }
        userName = Session["userName"].ToString();
        if (!IsPostBack)
        {
            ReadUserInfo();
        }
    }



    protected void ReadUserInfo()
    {
        DataSet ds = currentUser.ReadAllUsers();
        dgUsers.DataSource = ds.Tables["userInfo"];
        dgUsers.DataBind();

    }

    //获取用户所在的部门
    protected void dgUsers_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.AlternatingItem:
            case ListItemType.Item:
                string depID = e.Item.Cells[0].Text;
                string findDepName = "select DEPNAME from DEPARTMENT where DEPID=" + depID;
                string depName = db.ExcuteScalar(findDepName).ToString();
                Label lblDepName =(Label) e.Item.FindControl("lblDepName");
                lblDepName.Text = depName;
                string userName = e.Item.Cells[1].Text;
                if (userName == "admin")
                {
                    RadioButton rBtnYes = (RadioButton)e.Item.FindControl("rBtnYes");
                    RadioButton rBtnNo = (RadioButton)e.Item.FindControl("rBtnNo");
                    Button btnDelele = (Button)e.Item.FindControl("btnDeleteUser");
                    rBtnYes.Enabled = false;
                    rBtnNo.Enabled = false;
                    btnDelele.Enabled = false;
                }
                break;
        }


    }
    //重置密码、删除用户方法
    protected void dgUsers_ItemCommand(object source, DataGridCommandEventArgs e)
    {
       
        switch (e.CommandName)
        {
            case "resetPwd":
                string passwd = Encrypt.toMD5("000000");
                string strResetPwd = "update USERS set PASSWD='" + passwd + "' where USERNAME='" + e.Item.Cells[1].Text + "'";
                if (db.ExcuteSql(strResetPwd))
                {
                    Response.Write("<script language='javascript'>alert('重置成功，密码为“000000”！')</script>");
                }
                else
               {
                     Response.Write("<script language='javascript'>alert('数据库尝试更新时，重置失败，请联系管理员！')</script>");
               }

                break;
            case "deleteUser":
                RadioButton rBtnYes = (RadioButton)e.Item.FindControl("rBtnYes");
                int i = 0;
                if(rBtnYes.Checked)
                {
                    i = 1;
                }
                string userName = e.Item.Cells[1].Text;
                if(Users.DeleteUser(userName,i))
                {
                    Response.Write("<script language='javascript'>alert('删除成功！')</script>");
                    ReadUserInfo();

                }
                else
                {
                    Response.Write("<script language='javascript'>alert('在尝试对数据库进行操作时，发生未知以外，删除失败！，请联系管')</script>");
                }
                break;
        }
    }
    protected void dgUsers_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        dgUsers.CurrentPageIndex = e.NewPageIndex;
        ReadUserInfo();
    }
}
